package com.x.mvp.utils;

import android.text.format.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static final String SDF_YYYYMMDD = "yyyy-MM-dd";
    public static final String SDF_YMDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String SDF_MMSS = "mm:ss";
    public static final String SDF_HHMM = "HH:mm";
    public static final String SDF_MDHHSS = "MM-dd HH:mm";
    public static final String SDF_MMDD = "M.d";
    public static final String SDF_HHMMSS = "HH:mm:ss";

    public static final String SDF_YYYYMMDD_LOAN = "yyyy/MM/dd";

    private static final int YEAR = 365 * 24 * 60 * 60;// 年
    private static final int MONTH = 30 * 24 * 60 * 60;// 月
    private static final int DAY = 24 * 60 * 60;// 天
    private static final int HOUR = 60 * 60;// 小时
    private static final int MINUTE = 60;// 分钟
    private static final int SECOND = 1;//秒
    private static SimpleDateFormat dateFormat = new SimpleDateFormat(SDF_YYYYMMDD);
    private static SimpleDateFormat dateMdhhssFormat = new SimpleDateFormat(SDF_MDHHSS);
    private static SimpleDateFormat dateMmssFormat = new SimpleDateFormat(SDF_MMSS);
    private static SimpleDateFormat dateHhmmFormat = new SimpleDateFormat(SDF_HHMM);
    private static SimpleDateFormat dateMmddFormat = new SimpleDateFormat(SDF_MMDD);
    private static SimpleDateFormat dateHHmmssFormat = new SimpleDateFormat(SDF_HHMMSS);
    private static SimpleDateFormat dateLoanFormat = new SimpleDateFormat(SDF_YYYYMMDD_LOAN);

    public static String format(String dateFormat, Date date) {
        return String.valueOf(DateFormat.format(dateFormat, date));
    }

    public static String getLoanEndtime(long days) {
        long currentTime = System.currentTimeMillis();
        currentTime = currentTime + days * 24 * 60 * 60 * 1000;

        Date date = new Date(currentTime);

        String end = dateLoanFormat.format(date);

        return end + "前";

    }

    public static String getLoantime(long timestamp) {

        return dateLoanFormat.format(new Date(timestamp * 1000));
    }

    public static int getExpireTime(long timestamp) {
        long currentTime = System.currentTimeMillis();
        timestamp *= 1000;
        if (currentTime > (timestamp)) {
            return ((int) (currentTime - timestamp) / 1000 / 60 / 60 / 24);
        } else {
            return ((int) (timestamp - currentTime) / 1000 / 60 / 60 / 24);
        }
    }

    public static String getEndingTime(long timestamp) {
        long currentTime = System.currentTimeMillis();
        long timeGap = (timestamp * 1000 - currentTime) / 1000;
        String timeStr = "";
        if (timestamp == 0) {
            timeStr = "永久";
        } else if (timeGap > YEAR) {
            timeStr = timeGap / YEAR + "年后过期";
        } else if (timeGap > MONTH) {
            timeStr = timeGap / MONTH + "个月后过期";
        } else if (timeGap > DAY) {// 1天以上
            timeStr = timeGap / DAY + "天后过期";
        } else if (timeGap > HOUR) {// 1小时-24小时
            timeStr = timeGap / HOUR + "小时后过期";
        } else {
            timeStr = "即将到期";
        }
        return timeStr;
    }

    public static String getRangeTime(long startTime, long endTime) {
        String timeStr = "";
        if (endTime == 0) {
            timeStr = "永久";
        } else {
            String start = dateFormat.format(new Date(startTime * 1000));
            String end = dateFormat.format(new Date(endTime * 1000));
            timeStr = start + " ~ " + end;
        }
        return timeStr;
    }

    public static String getUserStr(long time) {
        String str = "";
        Date tDate = new Date(time);
        int t = getYearDay(time);
        int c = getYearDay(System.currentTimeMillis());
        if (t <= c) {
            int s = c - t;
            if (s <= 2) {
                switch (s) {
                    case 0:
                        str = "今天";
                        break;
                    case 1:
                        str = "昨天";
                        break;
                    case 2:
                        str = "前天";
                        break;
                }
            } else {
                str = getSdfYyyymmdd(tDate);
            }
        }
        return str;
    }

    public static String getWeekZh(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String weekZh;
        switch (dayOfWeek) {
            case Calendar.MONDAY:
                weekZh = "周一";
                break;
            case Calendar.TUESDAY:
                weekZh = "周二";
                break;
            case Calendar.WEDNESDAY:
                weekZh = "周三";
                break;
            case Calendar.THURSDAY:
                weekZh = "周四";
                break;
            case Calendar.FRIDAY:
                weekZh = "周五";
                break;
            case Calendar.SATURDAY:
                weekZh = "周六";
                break;
            case Calendar.SUNDAY:
            default:
                weekZh = "周日";
                break;
        }
        return weekZh;
    }

    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        return month;
    }

    public static int getYear() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        int month = calendar.get(Calendar.YEAR);
        return month;
    }

    public static int getDay() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        int month = calendar.get(Calendar.DAY_OF_MONTH);
        return month;
    }

    public static int getYearDay(long time) {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date(time);
        calendar.setTime(date);
        int month = calendar.get(Calendar.DAY_OF_YEAR);
        return month;
    }

    public static String getMmss(Date date) {
        String mmss = dateMmssFormat.format(date);
        return mmss;
    }

    public static String getHhmm(Date date) {
        String hhmm = dateHhmmFormat.format(date);
        return hhmm;
    }

    public static String getSdfYyyymmdd(Date date) {
        String yyyymmdd = dateFormat.format(date);
        return yyyymmdd;
    }

    public static String getSdfMdhhss(Date date) {
        String yyyymmdd = dateMdhhssFormat.format(date);
        return yyyymmdd;
    }

    public static String getSdfMmdd(Date date) {
        String mmdd = dateMmddFormat.format(date);
        return mmdd;
    }

    public static String getSdfHhmmss(Date date) {
        String mmdd = dateHHmmssFormat.format(date);
        return mmdd;
    }
}
